import CafeApi from '../api/cafe';
import fa from "element-ui/src/locale/lang/fa";

export const cafes = {
    state: {
        page: 1,

        cafes: [],
        cafesLoadStatus: 0,

        cafe: {},
        cafeLoadStatus: 0,

        cafeCreateStatus: 0,
        cafeCreated: {},
        cafeCreatedText: '',

        cafeDeletedStatus: 0,
        cafeDeletedText: '',

        cafeLikeActionStatus: 0,
        cafeUnlikeActionStatus: 0,
        cafeLiked: false,

        cafesView: 'map',
    },
    actions: {
        //列表
        loadCafes({commit, dispatch}) {
            commit('setCafesLoadStatus', 1);
            CafeApi.getCafes().then(function (response) {
                //console.log(response.data.data.data);
                commit('setCafes', response.data.data);
                //排序
                dispatch('orderCafes', {
                    order: rootState.filters.orderBy,
                    direction: rootState.filteres.orderDirection,
                });
                commit('setCafesLoadStatus', 2);
            }).catch(function () {
                commit('setCafes', []);
                commit('setCafesLoadStatus', 3);
            })
        },
        //详情
        loadCafe({commit}, data) {
            commit('setCafeLoadStatus', 1);
            CafeApi.getCafe(data.id).then(function (response) {
                //console.log(response);
                //console.log('loaded..');
                commit('setCafe', response.data.data);
                commit('setCafeLoadStatus', 2);
                commit('setCafeLiked', response.data.data.user_liked);

            }).catch(function () {
                commit('setCafe', {});
                commit('setCafeLoadStatus', 3);
            })
        },
        //创建
        createCafe({commit, state, dispatch}, data) {
            commit('setCafeCreateStatus', 1);
            CafeApi.createCafe(data.name, data.website, data.description, data.roaster, data.locations, data.photos)
                .then(function (response) {
                    commit('setCafeCreateStatus', 2);
                    dispatch('loadCafes');
                    this.$router.push({name: 'home'})
                })
                .catch(function () {
                    commit('setCafeCreateStatus', 3);
                })
        },
        //喜欢
        likeCafe({commit}, data) {
            commit('setCafeLikeActionStatus', 1);
            CafeApi.likeCafe(data.id).then(function (response) {
                commit('setCafeLikeActionStatus', 2);
                commit('setCafeLiked', true);
            }).catch(function (error) {
                commit('setCafeLikeActionStatus', 3);
                console.log(error)
            })
        },
        //取消喜欢
        unlikeCafe({commit}, data) {
            commit('setCafeUnlikeActionStatus', 1);
            CafeApi.unlikeCafe(data.id).then(function (response) {
                commit('setCafeUnlikeActionStatus', 2);
                commit('setCafeLiked', false);
            }).catch(function (error) {
                commit('setCafeUnlikeActionStatus', 3);
                console.log(error)
            })
        },
        changeCafesView({commit},data){
            commit('setCafesView',data.view)
        },
        //cafe排序
        orderCafes({commit, state, dispatch}, data) {
            let localCafes = state.cafes;
            switch (data.order) {
                case 'name':
                    localCafes.sort(function (a, b) {
                        if (data.direction === 'desc') {
                            return ((a.company.name === b.company.name) ? 0 : ((a.company.name < b.company.name) ? 1 : -1));
                        } else {
                            return ((a.company.name === b.company.name) ? 0 : ((a.company.name > b.company.name) ? 1 : -1));
                        }
                    });
                    break;
                case 'most-liked':
                    localCafes.sort(function (a, b) {
                        if (data.direction === 'desc') {
                            return ((a.likes_count === b.likes_count) ? 0 : (a.likes_count < b.likes_count) ? 1 : -1);
                        } else {
                            return ((a.likes_count === b.likes_count) ? 0 : (a.likes_count > b.likes_count) ? 1 : -1);
                        }
                    });
                    break;
            }
            commit('setCafes',localCafes);
        }
    },
    mutations: {
        setCafesLoadStatus: function (state, status) {
            state.cafesLoadStatus = status;
        },
        setCafes: function (state, cafes) {
            if (cafes) {
                state.cafes = cafes;
            }
        },
        setCafeLoadStatus: function (state, status) {
            //console.log('set load status:'+status);
            state.cafeLoadStatus = status;
        },
        setCafe: function (state, cafe) {
            state.cafe = cafe;
        },
        setCafeCreateStatus: function (state, status) {
            state.cafeCreateStatus = status;
        },
        setCafeCreatedText:function(state,text){
            state.cafeCreatedText = text;
        },
        setCafeLikeActionStatus: function (state, status) {
            state.cafeLikeActionStatus = status;
        },
        setCafeUnlikeActionStatus: function (state, status) {
            state.cafeUnlikeActionStatus = status;
        },
        setCafeLiked: function (state, status) {
            state.cafeLiked = status;
        },
        setCafesView:function (state,view) {
            state.cafesView = view;
        }
    },
    getters: {
        getCafesLoadStatus(state) {
            return state.cafesLoadStatus;
        },
        getCafes(state) {
            return state.cafes;
        },
        getCafeLoadStatus(state) {
            //console.log('get load status:'+state.cafeLoadStatus);
            return state.cafeLoadStatus;
        },
        getCafe(state) {
            return state.cafe;
        },
        getCafeCreateStatus(state) {
            return state.cafeCreateStatus;
        },
        getCafeCreatedText(state){
          return state.cafeCreatedText;
        },
        getCafeLikeActionStatus(state) {
            return state.cafeLikeActionStatus;
        },
        getCafeUnlikeActionStatus(state) {
            return state.cafeUnlikeActionStatus;
        },
        getCafeLiked(state) {
            return state.cafeLiked;
        },
        getCafesView(state){
            return state.cafesView;
        }

    }
};
